package com.jar.exercise.网络编程.UDP连接.threadsocket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @author 松鼠
 * @date 2019/6/30 -  18:56
 */
public class Server implements Runnable {
	private DatagramSocket datagramSocket;

	public Server(DatagramSocket datagramSocket) {
		this.datagramSocket = datagramSocket;
	}

	@Override
	public void run() {
		try {
			byte[] b = new byte[1024];
			DatagramPacket datagramPacket = new DatagramPacket(b, b.length);
			while (true) {
				datagramSocket.receive(datagramPacket);
				byte[] data = datagramPacket.getData();
				System.out.println(datagramPacket.getAddress() + "::" + new String(data, 0, datagramPacket.getLength()));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
